package com.example.fw;

public class ContactHelper extends HelperBase {
    ContactHelper(ApplicationManager manager){
        super(manager);
    }

    private AutoItHelper submitContactCreation() {
        return appManager.getAutoItHelper()
                .click("Save")
                .winWaitAndActivate("AddressBook Portable", "", 5000);
    }

    private AutoItHelper fillContactForm(ContactData contactData) {
        return appManager.getAutoItHelper()
                .send("TDBEdit12", contactData.getFirstName())
                .send("TDBEdit11", contactData.getLastName());
    }

    private AutoItHelper initContactCreation() {
        return appManager.getAutoItHelper()
                .winWaitAndActivate("AddressBook Portable", "", 5000)
                .click("Add")
                .winWaitAndActivate("Add Contact", "", 5000);
    }

    private AutoItHelper cancelContactModification() {
        return appManager.getAutoItHelper()
                .click("Cancel")
                .winWaitAndActivate("Update Contact", "", 5000);
    }

    private ContactData getContactDataFromCard() {
        return new ContactData()
                .setFirstName(appManager.getAutoItHelper().getText("TDBEdit12"))
                .setLastName(appManager.getAutoItHelper().getText("TDBEdit11"));
    }

    private AutoItHelper initFirstContactUpdate() {
        selectFirstContact();
        return appManager.getAutoItHelper().winWaitAndActivate("Update Contact", "", 5000);
    }

    private AutoItHelper selectFirstContact() {
        return appManager.getAutoItHelper()
                .winWaitAndActivate("AddressBook Portable", "", 5000)
                .focus("TListView1")
                .send("{DOWN}{SPACE}")
                .click("Edit");
    }

    private AutoItHelper deleteSelectedContact() {
        return appManager.getAutoItHelper()
                .click("Delete")
                .winWaitAndActivate("Confirm", "", 5000)
                .send("{SPACE}")
                .winWaitAndActivate("AddressBook Portable", "", 5000);
    }

    public ContactData getFirstContact() {
        initFirstContactUpdate();

        ContactData contactData = getContactDataFromCard();

        cancelContactModification();

        return contactData;  //To change body of created methods use File | Settings | File Templates.
    }

    public ContactHelper createContact(ContactData contactData) {

        initContactCreation();

        fillContactForm(contactData);

        submitContactCreation();

        return this;
    }

    public ContactHelper deleteContact() {

        selectFirstContact();

        deleteSelectedContact();
        return this;
    }
}
